package com.trkj.controller.hjj;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.trkj.pojo.Calcluaterule;
import com.trkj.pojo.StaffInformation;
import com.trkj.service.ServiceException;
import com.trkj.service.hjj.CalcluateRuleService_lxlk;
import com.trkj.vo.Result_AddRule_Result;

@Controller
public class CalcluateruleController_lxlk{
	@Autowired
	private CalcluateRuleService_lxlk calcluateService;
	
	@RequestMapping("to_calcluatestamp")
	public String ToHoliday(){
		return "hjj/calcluate_rule_lxl";
	}
	/**
	 * 查询全部
	 * @param page
	 * @param rows
	 * @return
	 */
	@PostMapping("to_calcluate_findall")
	@ResponseBody
	public Map findAllCalcluate(int page,int rows){
		long total = calcluateService.CalcluatePageCount();
		List<Calcluaterule> list = calcluateService.findAllCalcluate(page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	/**
	 * 模糊查询
	 * @param calcluateruleName
	 * @param page
	 * @param rows
	 * @return
	 */
	@PostMapping("to_calcluate_like")
	@ResponseBody
	public Map findAllCalcluate(String calcluateruleName,int page,int rows){
		long total = calcluateService.CalcluatePageCount();
		List<Calcluaterule> list = calcluateService.ByNameCalcluate(calcluateruleName, page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	/**
	 * 新增
	 * @param request
	 * @param calcluateruleName
	 * @param calcluateruleCount
	 * @param calcluateruleRemarks
	 * @param calcluateruleSj
	 * @param calcluateruleState
	 * @return
	 */
	@RequestMapping("to_calcluate_add")
	@ResponseBody
	public Result_AddRule_Result addCalcluate(HttpSession session,String calcluateruleName,String calcluateruleCount,String calcluateruleRemarks,
			Integer calcluateruleSj,Integer calcluateruleState){
		try {
			StaffInformation staff = (StaffInformation) session.getAttribute("staff");
			calcluateService.addCalcluate(staff.getStainName(), calcluateruleName, calcluateruleCount, calcluateruleRemarks, calcluateruleSj, calcluateruleState);
			return Result_AddRule_Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	
	/**
	 * 编辑
	 * @param calcluateruleId
	 * @param calcluate
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("to_calcluate_edit")
	@ResponseBody
	public Result_AddRule_Result editCalcluate(Long calcluateruleId,Calcluaterule calcluate) throws ServiceException{
		try {
			calcluateService.editCalcluate(calcluateruleId, calcluate);
			return Result_AddRule_Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	
	/**
	 * 修改状态
	 * @param calcluateruleId
	 * @param calcluateruleState
	 * @return
	 */
	@RequestMapping("to_calcluate_editstate")
	@ResponseBody
	public Result_AddRule_Result update_CalcluateruleState(Long calcluateruleId,Integer calcluateruleState){
		try {
			calcluateService.update_CalcluateruleState(calcluateruleId, calcluateruleState);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
		
	}
	
	/**
	 * 加载
	 * @param calcluateruleId
	 * @return
	 */
	@RequestMapping("to_calcluate_load")
	@ResponseBody
	public Calcluaterule loadCalcluaterule(Long calcluateruleId){
		return calcluateService.loadCalcluaterule(calcluateruleId);
	}
	
	/**
	 * 删除
	 * @param calcluateruleId
	 * @return
	 */
	@RequestMapping("to_calcluate_delete")
	@ResponseBody
	public Result_AddRule_Result delete_Calcluaterule(Long calcluateruleId){
		try {
			int ca = calcluateService.delete_Calcluaterule(calcluateruleId);
			if(ca==1){
				return Result_AddRule_Result.SUCCESS;
			}
			return Result_AddRule_Result.failure("计算规则正在被引用不能删除！！！");
		} catch (Exception e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
}
